﻿
@{
    ViewData["Title"] = "BookAdd";
    Layout = "~/Views/Shared/_SmallWinLayout.cshtml";
}

<div class="page-container">
    <div   class="form form-horizontal " id="form-article-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>图书名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="" placeholder="请输入图书名称" id="" name="bookname">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>ISBN号：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="" placeholder="ISBN信息 至少十位" id="" name="ISBN">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>图书分类：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="中国长篇小说" placeholder="" id="" name="type">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>预发行库存量：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="1000" placeholder="1000" id="" name="inventory">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>开本：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="format" id="" placeholder="16开" value="16开" class="input-text auth">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>总页数：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="pagecount" id="" placeholder="页数" value="" class="input-text auth">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>总字数：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="wordcount" id="" placeholder="字数" value="" class="input-text auth">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>图书价格：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="price" id="" placeholder="30.00" value="30.00" class="input-text auth" style="width:90%">
                元
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>稿费：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="fee" id="" placeholder="500" value="500" class="input-text auth" style="width:90%">
                元
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>责任编辑：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="edit" id="" placeholder="" value="陈行行" class="input-text auth" style="width:40%">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>预出版日期：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="publicdate" placeholder="格式：20180808" onfocus="WdatePicker({ dateFmt:'yyyyMMdd',maxDate:'#F{$dp.$D(\'datemax\')||\'%y%M%d\'}' })" id="datemin" class="input-text Wdate auth" style="width:60%">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>图书简介：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <textarea name="content" cols="" rows="" id="bookcontent" class="textarea auth" placeholder="说点什么...最少输入10个字符" datatype="*10-100" dragonfly="true" nullmsg="备注不能为空！" onKeyUp="$.Huitextarealength(this,200)"></textarea>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button  onclick="book_add('/Home/BookAddSave')" id="bookadd" class="btn btn-secondary radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 保存</button>
                <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">

    //图书增加的验证方法
    function book_add(url) {

        $('input:first').focus();
        $('input[name = bookname]').focus(function () {
            $(this).attr('placeholder', '请输入图书名称');
        });
        $('input[name = ISBN]').focus(function () {
            $(this).attr('placeholder', '图书的ISBN信息：例如 9787544726313')
        });
        $('input[name = type]').focus(function () {
            $(this).attr('placeholder', '图书的类别：例如 小说');
        });
        $('input[name = inventory]').focus(function () {
            $(this).attr('placeholder', '预出版库存量信息：建议 10000');
        });
        $('input[name = format]').focus(function () {
            $(this).attr('placeholder', '开本信息：16开');
        });
        $('input[name = pagecount]').focus(function () {
            $(this).attr('placeholder', '总页数');
        });
        $('input[name = wordcount]').focus(function () {
            $(this).attr('placeholder', '总字数');
        });
        $('input[name = price]').focus(function () {
            $(this).attr('placeholder', '预售价格');
        });
        $('input[name = fee]').focus(function () {
            $(this).attr('placeholder', '稿费信息');
        });
        $('input[name = publicdate]').focus(function () {
            $(this).attr('placeholder', '预出版日期：格式 年年年年月月日日 例:20190705');
        });
        $('#bookcontent').focus(function () {
            $(this).attr('placeholder', '图书简介信息');
        });
        //layer.msg("保存信息");
        var bookList = new Array();
        $('input[name = bookname]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '图书名不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = ISBN]').blur(function () {
            val = this.value;
            if (val.length < 10) {
                $(this).attr('placeholder', 'ISBN位数至少为10位');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = type]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '图书类型不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = inventory]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '库存量不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = format]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '开本信息不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = pagecount]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '总页数不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = wordcount]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '总字数不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = price]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '价格不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = fee]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '稿费不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });

        $('input[name = edit]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '编辑不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });

        $('input[name = publicdate]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '预出版日期不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('#bookcontent').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '内容简介不能为空');
                $(this).data({ 's': '0' });
            }
            else {
                bookList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });

        layer.confirm('确认要保存吗？', function () {
            bookList.length = 0;
            $('.auth').blur();
            tot = 0;
            $('.auth').each(function () {
                if ($(this).data('s') == 1) {
                    tot += 1;
                }
            });
            if (tot != 12) {
                //alert(tot);
                layer.msg('请完善图书信息再提交！');
            }
            else {
                //alert(tot);
                //已完成：添加，修改之前验证是否已存在相同的图书，ISBN唯一
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { 'additems': bookList.toString() },
                    success: function (id) {
                        if (id == "errorISBN") {
                            layer.msg('ISBN重复了，请重新编辑', { icon: 5, time: 4000 });
                            bookList.length = 0;  //数组清空
                        }
                        else if (id == "errorName") {
                            layer.msg('图书名字已存在，请重新编辑', { icon: 5, time: 4000 });
                            bookList.length = 0;  //数组清空
                        }
                        else if (id != "false") {
                            layer.msg('已添加成功,图书ID为' + id, { icon: 1, time: 5000 });
                            self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                                window.parent.location.reload();   // 这个方法是刷新当前页面
                            }, 2000); //这里2000代表两秒
                        }
                        else {
                            layer.msg('添加失败，请重新编辑', { icon: 5, time: 4000 });
                            bookList.length = 0;  //数组清空
                            //self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                            //    window.location.reload();   // 这个方法是刷新当前页面
                            //}, 2000) //这里2000代表两秒
                        }
                    },
                    error: function () {
                        layer.msg('保存失败了，请重新尝试编辑一下', { icon: 5, time: 4000 });
                    },
                });
            }
          
        });        
    }
</script>